//
//  ScrollViewVC_1.m
//  UILbrary
//
//  Created by Apple on 2022/5/8.
//  Copyright © 2022 mac. All rights reserved.
//

#import "ScrollViewVC_1.h"
#import "MYHeader.h"

typedef NS_ENUM(NSInteger, ScrollviewType) {
    ScrollviewType_Vertical,
    ScrollviewType_Horizontal
};

@interface ScrollViewVC_1 ()

@end

@implementation ScrollViewVC_1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self p_scrollViewDemo_Horizontal];
    [self p_scrollViewDemo_Vertical];
}

// showsVerticalScrollIndicator
// showsHorizontalScrollIndicator

/// 横向滚动
- (void)p_scrollViewDemo_Horizontal
{
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];

    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_topMargin).offset(10);
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
        make.height.mas_equalTo(200);
    }];
    [self p_scrollView:scrollView scrollType:ScrollviewType_Horizontal];
}

/// 纵向滚动
- (void)p_scrollViewDemo_Vertical
{
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];

    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_topMargin).offset(220);
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
        make.height.mas_equalTo(200);
    }];
    [self p_scrollView:scrollView scrollType:ScrollviewType_Vertical];
}


- (void)p_scrollView:(UIScrollView *)scrollView scrollType:(ScrollviewType )type
{
    
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:contentView];
    
    [self.view layoutIfNeeded];
    [scrollView layoutIfNeeded];
    CGRect scrollView_frame = scrollView.frame;
    CGFloat contentView_width = scrollView_frame.size.width - 10;
    CGFloat contentView_height = scrollView_frame.size.height - 10;

    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(scrollView).inset(15.f);
            make.left.equalTo(scrollView).inset(15.f);
            make.right.equalTo(scrollView).inset(-15.f);
            make.bottom.equalTo(scrollView).inset(-15.f);
        if (type == ScrollviewType_Vertical) {
            make.width.mas_equalTo(contentView_width);
            make.height.greaterThanOrEqualTo(@0);
        }else{
            make.height.mas_equalTo(contentView_height);
            make.width.greaterThanOrEqualTo(@0);
        }
            
    }];
    
    NSMutableArray *labelViews = [NSMutableArray arrayWithCapacity:1];
        for (int i = 0; i < 20; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor blackColor];
            label.backgroundColor = [UIColor randomColor];
            label.text = [NSString stringWithFormat:@"我是Label,索引值为:%d",i+1];
            label.font = [UIFont boldSystemFontOfSize:16.f];
            [contentView addSubview:label];
            [labelViews addObject:label];
        }
    UILabel *prev = nil;
        for (int i = 0; i < labelViews.count; i++) {
            if (!prev) {
                [labelViews[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                    if (type == ScrollviewType_Vertical) {
                        make.top.left.right.equalTo(contentView);
                        make.height.mas_equalTo(100);

                    }else{
                        make.top.bottom.left.equalTo(contentView);
                        make.width.mas_equalTo(100);
                    }

                }];
            } else {
                [labelViews[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                    if (type == ScrollviewType_Vertical) {
                        make.top.equalTo(prev.mas_bottom);
                        make.left.right.equalTo(prev);
                        make.height.mas_equalTo(100);
                        if (i == labelViews.count - 1) {
                            make.bottom.equalTo(contentView.mas_bottom);
                        }
                    }else{
                        make.left.equalTo(prev.mas_right);
                        make.top.bottom.equalTo(prev);
                        make.width.mas_equalTo(100);
                        if (i == labelViews.count - 1) {
                            make.right.equalTo(contentView.mas_right);
                        }
                    }
                    
                }];
            }
            prev = labelViews[i];
        }

    
}


@end
